package app.beit.fintrack.controller.search;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import app.beit.fintrack.bean.summary.SummarySessionBean;
import app.beit.fintrack.bean.summary.SummaryTrackPidDataBean;
import app.beit.fintrack.controller.BaseController;
import app.beit.fintrack.controller.ResponseFintrack;
import app.beit.fintrack.service.summarytrack.SummaryTrackService;
import app.beit.fintrack.utils.DateFormatUtils;
import app.beit.fintrack.utils.constr.MsgConstr;
import app.beit.fintrack.utils.constr.SessionConstr;

@Controller
@RequestMapping("sssummarypid")
public class bak_SummaryPIDController extends BaseController {
	private final Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private SummaryTrackService summaryTrackService;

	@RequestMapping
	public String excute(final HttpServletRequest request, final @RequestParam(value="c", required=false) String c) {
		if(c == null){
			request.getSession().removeAttribute(SessionConstr.SESSION_SUMMARY_PID);
		}
		return "summarypid/summarypid";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "searchByPID", method = RequestMethod.POST)
	@ResponseBody
	public ResponseFintrack<Boolean> searchByType(
			final HttpServletRequest request,
			final @RequestParam("hospitalCode") String hospitalCode,
			final @RequestParam("searchBy") String searchBy,
			final @RequestParam("value") String value,
			final @RequestParam("dateFromAdmidTime") String dateFromAdmidTime,
			final @RequestParam("dateToAdmidTime") String dateToAdmidTime) {
		this.log.info("SummaryPIDController Class");
		ResponseFintrack<Boolean> response = null;
		final SummarySessionBean ssb = new SummarySessionBean();

		try {
			Double total = 0d;
			Double amount = 0d;
			Double amountNotPassC = 0d;
			Double amountNotPass = 0d;

			int cntPersonNotPass = 0;
			int cntPersonPass = 0;
			int cntStandardNotPass = 0;
			int cntAppeal = 0;

			String dateFromBuddhist = null;
			String dateToBuddhist = null;
			String hcode = null;
			this.log.info(dateFromAdmidTime);
			if (dateFromAdmidTime != null && !dateFromAdmidTime.equals("")) {
				String[] split = dateFromAdmidTime.split("/");
				if (split.length > 2 && !split[2].equalsIgnoreCase("NaN")) {
					int year = Integer.parseInt(split[2]) + 543;
					log.info("Before : " + Integer.parseInt(split[2])
							+ ", After : " + Integer.parseInt(split[2]) + 534);
					dateFromBuddhist = split[0] + "/" + split[1] + "/" + year;
				}
			}
			if (dateToAdmidTime != null && !dateToAdmidTime.equals("")) {
				String[] split = dateToAdmidTime.split("/");
				if (split.length > 2 && !split[2].equalsIgnoreCase("NaN")) {
					int year = Integer.parseInt(split[2]) + 543;
					log.info("Before : " + Integer.parseInt(split[2])
							+ ", After : " + Integer.parseInt(split[2]) + 534);
					dateToBuddhist = split[0] + "/" + split[1] + "/" + year;
				}
			}
			if (hospitalCode != null && hospitalCode != "") {
				hcode = hospitalCode.substring(0, 5);
			}

			List<SummaryTrackPidDataBean> beans = this.summaryTrackService.getDetailsByPID(hcode, searchBy, value,
							dateFromBuddhist != null ? DateFormatUtils.strToDate(dateFromBuddhist) : null, 
									dateToBuddhist != null ? DateFormatUtils.strToDate(dateToBuddhist) : null, 0, 1000);

//			final List<SummaryTrackDetailsDataBean> newBeans = new ArrayList<SummaryTrackDetailsDataBean>();
			if (beans != null) {

				for (SummaryTrackPidDataBean bean : beans) {
					ssb.setFullName(bean.getFullname());
					ssb.setPid(bean.getPid());
					break;
				}

				for (final SummaryTrackPidDataBean b : beans) {
					final String beanStatus = b.getCodeId() == null ? "" : b
							.getCodeId();
					final String bStatus = b.getStatus() == null ? "" : b
							.getStatus();

					if (beanStatus.equalsIgnoreCase("DENY")
							&& !bStatus.equalsIgnoreCase("A")) {
						cntStandardNotPass++;
						amountNotPass += Double.parseDouble(b.getCompensation()
								.toString());
					} else if (bStatus.equalsIgnoreCase("A")) {
						cntAppeal++;
					} else if (!beanStatus.equalsIgnoreCase("")) {
						cntPersonNotPass++;
					}

					
					total += Double
							.parseDouble(b.getDischarge() == null
									|| b.getDischarge().toString()
											.equalsIgnoreCase("") ? "0" : b
									.getDischarge().toString());
					
//					amount += Double.parseDouble(b.getSumsAE() != null?b.getSumsAE().toString():"0")
//							+ Double.parseDouble(b.getSumsDMIS() != null?b.getSumsDMIS().toString():"0")
//							+ Double.parseDouble(b.getSumsHC() != null?b.getSumsHC().toString():"0")
//							+ Double.parseDouble(b.getSumsINST() != null?b.getSumsINST().toString():"0")
//							+ Double.parseDouble(b.getSumsIP() != null?b.getSumsIP().toString():"0")
//							+ Double.parseDouble(b.getSumsOP() != null?b.getSumsOP().toString():"0")
//							+ Double.parseDouble(b.getSumsOT() != null?b.getSumsOT().toString():"0")
//							+ Double.parseDouble(b.getSumsPA() != null?b.getSumsPA().toString():"0")
//							+ Double.parseDouble(b.getSumsDRUG() != null?b.getSumsDRUG().toString():"0");

				}

				cntPersonPass = (beans.size() - cntPersonNotPass) - cntStandardNotPass - cntAppeal;
			} else {
				beans = new ArrayList<SummaryTrackPidDataBean>();
			}

			// amount = amount - amountNotPass - amountNotPassC;

			// ssb.setLstSummaryDetailsDatas(newBeans);

			final List<SummaryTrackPidDataBean> lstBeans = new ArrayList<SummaryTrackPidDataBean>();
			for(final SummaryTrackPidDataBean b : beans){
				String rep = b.getRepNo();
				String pid = b.getPid();
//				String hn = b.getHn();
//				String an = b.getAn();
				String hCode = b.getHospitalCode();
//				String admidDate = b.getStrAdmidDate();
//				String dischargeDate = b.getStrDischargeDate();
//				final String key = rep+pid+hCode+admidDate+dischargeDate;
//				final String mainBatchId = b.getBatchId();

//				if(mainBatchId != null && mainBatchId.length() > 0){
//					for(final SummaryTrackDetailsDataBean sb : beans){
//						rep = sb.getRepNo();
//						pid = sb.getPid();
////						hn = sb.getHn();
////						an = sb.getAn();
//						hCode = sb.getHospitalCode();
//						admidDate = sb.getStrAdmidDate();
//						dischargeDate = sb.getStrDischargeDate();
//						final String subKey = rep+pid+hCode+admidDate+dischargeDate;
//						final String subBatchId = sb.getBatchId();
//						if(key.equalsIgnoreCase(subKey)){
//							if(!mainBatchId.equalsIgnoreCase(subBatchId)){
//								b.setBatchId(mainBatchId + ", " + subBatchId);	
//							}
//							boolean isDuplicate = false;
//							for(final SummaryTrackDetailsDataBean ssbeans : lstBeans){
//								rep = ssbeans.getRepNo();
//								pid = ssbeans.getPid();
////								hn = ssbeans.getHn();
////								an = ssbeans.getAn();
//								hCode = ssbeans.getHospitalCode();
//								admidDate = ssbeans.getStrAdmidDate();
//								dischargeDate = ssbeans.getStrDischargeDate();
//								final String ssKey = rep+pid+hCode+admidDate+dischargeDate;
//								if(key.equalsIgnoreCase(ssKey)){
//									isDuplicate = true;
//									break;
//								}
//							}
//							if(!isDuplicate){
//								lstBeans.add(b);
//							}
//						}
//					}
//				}else{
//					lstBeans.add(b);
//				}
			}
			
			cntAppeal = 0;
			cntPersonNotPass = 0;
			cntStandardNotPass = 0;
			for(final SummaryTrackPidDataBean b : lstBeans){
				final String beanStatus = b.getCodeId() == null ? "" : b.getCodeId();
				final String bStatus = b.getStatus() == null ? "" : b.getStatus();

				if (beanStatus.equalsIgnoreCase("DENY")
						&& !bStatus.equalsIgnoreCase("A")) {
					cntStandardNotPass++;
					amountNotPass += Double.parseDouble(b.getCompensation().toString());
				} else if (bStatus.equalsIgnoreCase("A")) {
					cntAppeal++;
				} else if (!beanStatus.equalsIgnoreCase("")) {
					cntPersonNotPass++;
				}
			}
			cntPersonPass = (lstBeans.size() - cntPersonNotPass) - cntStandardNotPass - cntAppeal;
			
			ssb.setLstSummaryDetailsDatas(lstBeans);
			ssb.setPersonNotPass(cntPersonNotPass);
			ssb.setPersonPass(cntPersonPass);
			ssb.setPersonAppeal(cntAppeal);
			ssb.setStandardNotPass(cntStandardNotPass);
			ssb.setTotal(total);
			ssb.setAmount(amount);
			ssb.setAmountNotPass(amountNotPass);
			ssb.setAmountNotPassC(amountNotPassC);
			
			ssb.setClickBack(true);

			request.getSession().setAttribute(SessionConstr.SESSION_SUMMARY_PID, ssb);

			this.log.info("end summary details data pid.........");
		} catch (Exception e) {
			this.log.error(e.getMessage());
			e.printStackTrace();
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE,
					e.getMessage());
		}

		return response;
	}

}
